﻿// mxml_test.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <stdio.h>
#include "xml_helper.h"

#include<stdio.h>
#include"mxml.h"

int main(void)
{
	FILE* fp = fopen("prac.xml", "r");
	//从prac.xml文件中加载xml
	mxml_node_t* xml = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK);
	//定义两个空节点
	mxml_node_t* book = NULL;
	mxml_node_t* DeviceList = NULL;
	mxml_node_t* Item = NULL;

	mxml_node_t* DeviceIDNode = mxmlFindElement(xml, xml, "DeviceID", NULL, NULL, MXML_DESCEND);
	const char* DeviceID = mxmlGetText(DeviceIDNode, NULL);
	printf("DeviceID:%s\n", DeviceID);
	//从xml开始向下查找 name=book attrr=category
	DeviceList = mxmlFindElement(xml, xml, "DeviceList", NULL, NULL, MXML_DESCEND);
	printf("DeviceList Num:%s\n", mxmlElementGetAttr(DeviceList, "Num"));
	Item = mxmlFindElement(DeviceList, xml, "Item", NULL, NULL, MXML_DESCEND);

	mxml_node_t* DeviceNameNode = mxmlFindElement(Item, DeviceList, "Name", NULL, NULL, MXML_DESCEND);
	const char* DeviceName = mxmlGetText(DeviceNameNode, NULL);

	mxmlDelete(xml);
	fclose(fp);
	return 0;
}

#if 0
/*
<?xml version="1.0" encoding="utf-8" ?>
<root>
<AAA>344</AAA>
<part id = "01" option="abbb">
<name>张三</name>
<age>20</age>
<sex>男</sex>
</part>
<listTest>
	<test>
		<x>1</x>
	</test>
	<test>
		<x>2</x>
	</test>
<listTest>
<arrList>
<arr>
	<xxx>abc</xxx>
	<yyy>1</yyy>
<arr>
<arr>
<xxx>cde</xxx>
<yyy>2</yyy>
<arr>
</arrList>
</root>
*/
int main()
{
	mxml_node_t* treeNode = CreateRootNode();
	mxml_node_t* rootNode = NULL;
	mxml_node_t* partNode = NULL;
	mxml_node_t* nameNode = NULL;
	mxml_node_t* ageNode = NULL;
	mxml_node_t* sexNode = NULL;
	mxml_node_t* listTestNode = NULL;
	mxml_node_t* arrListNode = NULL;
	mxml_node_t* testNode = NULL;
	mxml_node_t* arrNode = NULL;
	mxml_node_t* AANode = NULL;
	AddNode(treeNode, "root", &rootNode);
	AddNode(rootNode, "part", &partNode);
	AddElemStr(rootNode, "AA", "344", NULL);
	AddNode(rootNode, "listTest", &listTestNode);
	AddNode(rootNode, "arrList", &arrListNode);
	SetAttrStr(partNode, "id", "01");
	SetAttrStr(partNode, "option", "abbb");

	AddElemStr(partNode, "name", "张三", &nameNode);
	AddElemStr(partNode, "sex", "male", &sexNode);
	AddElemInt(partNode, "age", 20, &ageNode);

	for (int i = 0; i < 2; i++)
	{
		AddElemInt(listTestNode, "test", i + 1, NULL); //x
	}

	char tmpArr[100];
	for (int i = 0; i < 2; i++)
	{
		sprintf(tmpArr, "xvdwerwer_%d", i + 1);
		AddNode(arrListNode, "arr", &arrNode);
		AddElemStr(arrNode, "xxx", tmpArr, NULL);
		AddElemInt(arrNode, "yyy", i + 1, NULL);
	}

	char* pSt = XmlPrintNode(rootNode);

	DeleteNode(rootNode);


	//解析------------
	mxml_node_t* pRoot = ParseNode(pSt);
	char* pSt2 = PrintNode(pRoot);

	rootNode = NULL;
	listTestNode = NULL;
	testNode = NULL;
	int nRet = 0;
	nRet = GetSonNode(pRoot, "root", &rootNode);
	if (nRet != 0)
	{
		return -1;
	}
	//获取属性
	partNode = NULL;
	GetSonNode(rootNode, "part", &partNode);

	char sId[100] = { 0 };
	if (!AANode)
	{
		GetAttrStr(partNode, "id", sId);
	}
	//解析listTest
	nRet = GetSonNode(rootNode, "listTest", &listTestNode);
	if (nRet != 0)
	{
		return -1;
	}
	testNode = GetFirstChildWithName(listTestNode, "test");

	while (testNode)
	{
		int a;
		GetNodeInt(testNode, &a);

		testNode = GetNextSiblingWithName(testNode, "test");
	}
	//解析arrList
	nRet = GetSonNode(rootNode, "arrList", &arrListNode);
	if (nRet != 0)
	{
		return -1;
	}
	arrNode = GetFirstChildWithName(arrListNode, "arr");

	char sTmp[100];
	while (arrNode)
	{
		memset(sTmp, 0, 100);
		GetSonNodeValue_Str_Copy(arrNode, "xxx", sTmp, 100);
		int a;
		GetSonNodeValue_Int(arrNode, "yyy", &a);

		arrNode = GetNextSiblingWithName(arrNode, "arr");
	}

	DeleteNode(pRoot);
	return 0;
}
#endif